using Game.Framework;
using UnityEngine;

namespace Game.Framework
{
    public class AudioTool
    {
        public static AudioTool Instance { get; } = new();

        public bool MusicSwitch;
        public bool SoundSwitch;
        
        private AudioTool()
        {
            EventBus.Register<EventStorageReload>(OnEventStorageReload);
        }

        private void Release()
        {
            EventBus.UnRegister<EventStorageReload>(OnEventStorageReload);
        }

        private void OnEventStorageReload(EventStorageReload e)
        {
            if (!SoundSwitch)
            {
                AudioManager.PauseAll();
            }
        }

        public int PlaySound(AudioClip clip, bool loop = false)
        {
            if (!Application.isPlaying)
                return -1;
            
            if (!SoundSwitch) return -1;

            return AudioManager.PlaySound(clip, loop);
        }

        public int PlaySound(string filePath, bool loop = false)
        {
            if (!Application.isPlaying)
                return -1;
            
            if (!SoundSwitch) return -1;
            
            var hander = YooAssetManager.Instance.Package.LoadAssetSync<AudioClip>(filePath);
            AudioClip audioClip = hander.AssetObject as AudioClip;
            return PlaySound(audioClip, loop);
        }

        public void StopSound(int audioId)
        {
            AudioManager.StopSoundById(audioId);
        }

        public void PlayMusic(AudioClip clip)
        {
            if (!MusicSwitch) return;

            AudioManager.PlayMusic(clip);
        }

        public void PlayMusic(AudioClip clip, bool loop)
        {
            if (!MusicSwitch) return;

            AudioManager.PlayMusic(clip, 1f, loop, false);
        }

        public void StopAllMusic()
        {
            AudioManager.StopAllMusic();
        }

        public void StopAllSounds()
        {
            AudioManager.StopAllSounds();
        }

        public void ResumeAllSounds()
        {
            AudioManager.ResumeAllSounds();
        }

        public void PauseAllSounds()
        {
            AudioManager.PauseAllSounds();
        }

        public void PauseAllMusic()
        {
            AudioManager.PauseAllMusic();
        }

        public void ResumeAllMusic()
        {
            AudioManager.ResumeAllMusic();
        }
    }
}